package com.ciwong.mobilelib.utils;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.R;
import com.ciwong.mobilelib.application.BaseApplication;


/**
 * Created by keepon on 2018/2/9.
 */

public enum ToastUtil {
    INSTANCE;// 实现单例
    private Toast mToast;//在类前面声明吐司，确保在这个页面只有一个吐司
    private TextView mTvToast;
    private Handler handler = new Handler();
    private String TAG = "ToastUtil";


    private void toastInCenter(Context context, int stringId) {
        toastInCenter(context, context.getResources().getString(stringId));
    }

    public void toastInCenter(Context context, String content) {
        toastInCenter(context, content, Toast.LENGTH_SHORT);
    }

    public void toastInCenter(Context context, String content, int duration) {
        try {
            if (mToast == null) {
                mToast = new Toast(context.getApplicationContext());
                mToast.setGravity(Gravity.CENTER, 0, 0);//设置toast显示的位置，这是居中
                View _root = LayoutInflater.from(context).inflate(R.layout.toast_custom_common, null);//自定义样式，自定义布局文件
                mTvToast = (TextView) _root.findViewById(R.id.tvCustomToast);
                mToast.setView(_root);//设置自定义的view
            }
            if (duration == Toast.LENGTH_LONG)
                mToast.setDuration(Toast.LENGTH_LONG);//设置toast显示的时长
            else
                mToast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
            mTvToast.setText(content);//设置文本
            mToast.show();//展示toast
        } catch (Exception e) {
            Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
            Log.i(TAG, "toast Exception" + e.getMessage());
        }
    }

    //重新调整toast后样式
    public void toastCenterNoNetError() {
        String errorString = getString(R.string.connect_disable5);
        if (errorString == null)
            return;
        toastCenter(BaseApplication.getInstance(), errorString, Toast.LENGTH_SHORT, R.mipmap.ic_error);
    }

    public void toastCenterNoNetErrorOrServerError() {
        if (NetworkUtils.isOnline()) {
            ToastUtil.INSTANCE.toastCenterError(R.string.server_error);
        } else {
            ToastUtil.INSTANCE.toastCenterNoNetError();
        }
    }

    public void toastCenterError(String errorString) {
        toastCenter(BaseApplication.getInstance(), errorString, Toast.LENGTH_SHORT, R.mipmap.ic_error);
    }

    public void toastCenterError(int errorStringResId) {
        String errorString = getString(errorStringResId);
        if (errorString == null)
            return;
        toastCenter(BaseApplication.getInstance(), errorString, Toast.LENGTH_SHORT, R.mipmap.ic_error);
    }

    public void toastCenterErrorWithErrorCode(int errorStringResId, int errorCode) {
        if (errorCode == 17 || errorCode == 27 || errorCode == 10004) {
            return;
        }
        toastCenterError(errorStringResId);
    }

    public void toastCenterErrorWithErrorCode(String errorString, int errorCode) {
        if (errorCode == 17 || errorCode == 27 || errorCode == 10004) {
            return;
        }
        toastCenterError(errorString);
    }

    public void toastCenterSuccess(String successString) {
        toastCenter(BaseApplication.getInstance(), successString, Toast.LENGTH_SHORT, R.mipmap.ic_success);
    }

    public void toastCenterSuccess(int successStringResId) {
        String successString = getString(successStringResId);
        if (successString == null)
            return;
        toastCenter(BaseApplication.getInstance(), successString, Toast.LENGTH_SHORT, R.mipmap.ic_success);
    }

    public void toastCenterDelay(String successString) {
        toastCenter(BaseApplication.getInstance(), successString, Toast.LENGTH_SHORT, R.mipmap.ic_look_delay);
    }

    public void toastCenterDelay(int successStringResId) {
        String successString = getString(successStringResId);
        if (successString == null)
            return;
        toastCenter(BaseApplication.getInstance(), successString, Toast.LENGTH_SHORT, R.mipmap.ic_look_delay);
    }

    @Nullable
    private String getString(int successStringResId) {
        BaseApplication baseApplication = BaseApplication.getInstance();
        if (baseApplication == null) {
            return null;
        }
        String successString = baseApplication.getString(successStringResId);
        return successString;
    }

    public void toastCenter(Context context, int stringId) {
        toastCenter(context, context.getResources().getString(stringId), Toast.LENGTH_SHORT, -1);
    }

    public void toastCenter(Context context, String content) {
        toastCenter(context, content, Toast.LENGTH_SHORT, -1);
    }

    public void toastCenter(Context context, int stringId, int resIcon) {
        toastCenter(context, context.getResources().getString(stringId), Toast.LENGTH_SHORT, resIcon);
    }

    public void toastCenter(Context context, CharSequence content, int resIcon) {
        toastCenter(context, content, Toast.LENGTH_SHORT, resIcon);
    }

    /**
     * @param context  上下文
     * @param content  显示的文本
     * @param resIcon  显示的图标 -1代表没有图标
     * @param duration 显示的时长
     */
    public void toastCenter(Context context, CharSequence content, int duration, int resIcon) {
        ImageView ivToast = null;
        TextView mTvToast = null;
        try {
            mToast = new Toast(context);
            mToast.setGravity(Gravity.CENTER, 0, 0);//设置toast显示的位置，这是居中
            View _root = LayoutInflater.from(context).inflate(R.layout.toast_custom_general, null);//自定义样式，自定义布局文件

            //图标处理
            ivToast = _root.findViewById(R.id.iv_toast);
            if (resIcon > 0) {
                ivToast.setVisibility(View.VISIBLE);
                ivToast.setImageResource(resIcon);
            } else {
                ivToast.setVisibility(View.GONE);
            }
            //文本处理
            mTvToast = (TextView) _root.findViewById(R.id.tv_toast);
            mTvToast.setText(content);//设置文本
            mToast.setView(_root);//设置自定义的view

            //显示时长处理
            mToast.setDuration(Toast.LENGTH_LONG);//默认设置短时间显示
            if (duration == Toast.LENGTH_LONG || duration == Toast.LENGTH_SHORT) {
                mToast.setDuration(duration);
            }
            show(mToast, duration);//展示toast
        } catch (Exception e) {
            Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
            Log.e(TAG, "toast Exception" + e.getMessage());
        }
    }

    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
            mTvToast = null;
        }
    }

    /**
     * 销毁toast 防止内存泄露
     *
     * @param toast
     * @param duration
     */
    private void show(final Toast toast, int duration) {
        long time = 0;
        if (toast != null) {
            toast.show();
        }
        if (duration == Toast.LENGTH_LONG)
            time = 4 * 1000;
        else if (duration == Toast.LENGTH_SHORT)
            time = 3 * 1000;
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                cancelToast(toast);
            }
        }, time);
    }

    private void cancelToast(Toast toast) {
        if (toast != null) {
            toast.cancel();
        }
    }
}
